package bdcin.mineracao;

import org.apache.lucene.analysis.Analyzer;
import bdcin.mineracao.analysis.br.*;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.FilterIndexReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;


public class SearchFiles {


  private static class OneNormsReader extends FilterIndexReader {
    private String field;

    public OneNormsReader(IndexReader in, String field) {
      super(in);
      this.field = field;
    }

    public byte[] norms(String field) throws IOException {
      return in.norms(this.field);
    }
  }

  private SearchFiles() {}

  public static void main(String[] args) throws Exception {
    
    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String normsField = null;
    index = "C:/temp/indices";
       
    IndexReader reader = IndexReader.open(index);
   
    
    if (normsField != null){
      reader = new OneNormsReader(reader, normsField);
    }
    Searcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new BrazilianAnalyzer();

    BufferedReader in = null;
    if (queries != null) {
      in = new BufferedReader(new FileReader(queries));
    } else {
      in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    }
      QueryParser parser = new QueryParser(field, analyzer);
    while (true) {
      if (queries == null)                        
        System.out.println("Buscar: ");

      String line = in.readLine();

      if (line == null || line.length() == -1)
        break;

      line = line.trim();
      if (line.length() == 0)
        break;
      
      Query query = parser.parse(line);
      System.out.println("Pesquisando por: " + query.toString(field));

      Hits hits = searcher.search(query);
      
      if (repeat > 0) {                           
        Date start = new Date();
        for (int i = 0; i < repeat; i++) {
          hits = searcher.search(query);
        }
        Date end = new Date();
        System.out.println("Tempo: "+(end.getTime()-start.getTime())+"ms");
      }

      System.out.println(hits.length() + " documentos coincidentes");

      final int HITS_PER_PAGE = 10;
      for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
        int end = Math.min(hits.length(), start + HITS_PER_PAGE);
        for (int i = start; i < end; i++) {

          if (raw) {                              
            System.out.println("doc="+hits.id(i)+" score="+hits.score(i));
            continue;
          }

          Document doc = hits.doc(i);
          String path = doc.get("path");
          if (path != null) {
            System.out.println((i+1) + ". " + path + " -- Score: "+hits.score(i));
            String title = doc.get("title");
            if (title != null) {
              System.out.println("   Titulo: " + doc.get("title"));              
            }
          } else {
            System.out.println((i+1) + ". " + "Nenhum path para esse documento");
          }
        }

        if (queries != null)                      
          break;
        
        if (hits.length() > end) {
          System.out.println("mais (y/n) ? ");
          line = in.readLine();
          if (line.length() == 0 || line.charAt(0) == 'n')
            break;
        }
      }
    }
    reader.close();
  }
}
